package com.atguigu.gulimall.controller;

import com.atguigu.gulimall.interceptor.CartInterceptor;
import com.atguigu.gulimall.service.CartService;
import com.atguigu.gulimall.vo.Cart;
import com.atguigu.gulimall.vo.CartItem;
import com.atguigu.gulimall.vo.UserInfoTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author ming
 * @create 2022/5/5
 * @description:
 */
@Slf4j
@Controller
public class CartController {
    @Autowired
    private CartService cartService;
    /**
     * 浏览器有一个cookie,user-key标识用户身份,一个月后过期
     * 如果第一次使用购物车功能,都会给一个临时的用户身份
     * 浏览器以后保存,每次访问都会带上这个cookie
     * 登录session有
     * 没登录 按照cookie里面带来的use-key来做
     *  第一次 如果没有临时用户,来帮忙创建一个临时用户
     * 
     * @return
     */
    @GetMapping("/cart.html")

   public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        //快速得到用户信息,ThreadLocal同一个线程共享数据
        //UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        Cart cart = cartService.getCart();
        model.addAttribute("cart",cart);
        return "cartList";
   }
   @GetMapping("/checkItem")
   public String checkedItem(@RequestParam("skuId") Long skuId, @RequestParam("checked") Integer check) {
      cartService.checkItem(skuId, check);
       return "redirect:http://cart.gulimall.com/cart.html";
   }
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num) {
        cartService.changeItem(skuId, num);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    @GetMapping("/deleteItem")
    public String deleteItem(@RequestParam("skuId") Long skuId) {
        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }
    /**
     * RedirectAttrbutes ra ra.addFlashAttrbute()将数据放入session在页面只能取出一次
     *                    ra.addAttrbute将数据放入url后面
     * @param skuId
     * @param num
     * @param attributes
     * @return
     * @throws ExecutionException
     * @throws InterruptedException
     */
   @GetMapping("/addToCart")
   public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes attributes) throws ExecutionException, InterruptedException {
       CartItem cartItem = cartService.addCart(skuId,num);
       attributes.addAttribute("skuId",skuId);
       return "redirect:http://cart.gulimall.com/addToCart.html";
   }
   @GetMapping("/addToCart.html")
   public String addToCartSuccessPage(@RequestParam("skuId") Long skuId,Model model){
        //重定向后成功页面再次查询购物车数据即可
       CartItem cartItem = cartService.getCartItem(skuId);
       model.addAttribute("item",cartItem);
       return "success";
   }
   @GetMapping("/currentUserCartItems")
   @ResponseBody
    public List<CartItem> currentUserCartItems(){
       return cartService.getCurrentUserCartItems();
   }
}               
